/**
 * 
 */
package jmine.tec.web.wicket.component.report;

import jmine.tec.report.api.Report;
import jmine.tec.report.export.ReportFileExporter;
import jmine.tec.web.wicket.spring.SpringBeanReference;

import org.apache.wicket.markup.html.WebResource;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.resource.IResourceStream;

/**
 * {@link WebResource} para exportacao de relatorios
 * 
 * @author takeshi
 */
public final class ExportReportWebResource extends WebResource {
    private final SpringBeanReference<? extends ReportFileExporter> reportExporter;

    private final Report report;

    /**
     * C'tor
     * 
     * @param reportExporter {@link SpringBeanReference} para reportExporter
     * @param report {@link Report}
     */
    public ExportReportWebResource(SpringBeanReference<? extends ReportFileExporter> reportExporter, Report report) {
        this.reportExporter = reportExporter;
        this.report = report;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IResourceStream getResourceStream() {
        return new ReportExportResourceStream(report, this.reportExporter.get());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void setHeaders(WebResponse response) {
        super.setHeaders(response);
        response.setContentType(reportExporter.get().getContentType());
        response.setAttachmentHeader(report.getHeader().getFileName() + reportExporter.get().getFileSuffix());
    }
}